|
|
|
|
|
|
|
Puzzle 26
Universal Identifiers, Part 2 |
|
|
|
|
|
|
|
|
The reason for the existence of universal identifiers is that there is no way to prevent two individuals from giving objects and interfaces the same name. For example, the default name for every Visual Basic project is Project1. The default name for the first class in such a project is Class1. If you create a quick ActiveX DLL for experimentation purposes and don't bother to change these names, you will create a server that exposes an object named Projectl.Class1. How many objects named Project1.Class1 are on your system? I don't even want to count the number drifting around on mine. And if you add up the number of objects with this name on all of the systems in the world . . . well, the prospect is too terrible to contemplate. |
|
|
|
|
|
|
|
|
The human-readable form of an object as described above is called a program ID, and you should strive to make each one unique. The program ID is stored in the registry so the CreateObject function can find the server that can create the object. But the developers of COM wanted to provide a way to uniquely identify objects even if they are assigned the same program ID. That's why each object is really identified by its CLSID value. The program ID in the registry only serves to find the CLSID for the object. The object is actually loaded using the CLSID, not the program ID. |
|
|
|
|
|
|
|
|
For example, if you use the CreateObject function to create a Microsoft Word document, you would use the program ID "Word.Document." Windows would then look up the string "Word.Document" in the system registry and find that the CLSID for the object is {00020906-0000-0000-C000-000000000046}. |
|
|
|
|
|
|
|
|
This means that there must be some way to look up the CLSID for a given program ID. You could, of course, look in the registry, but it turns out that there is an OLE function that does the work for you. |
|
|
|
|
|
|
|
|
The CLSIDFromProgID function is defined in the Win32 API documentation as follows: |
|
|
|
|
|
|
|
|
HRESULT CLSIDFromProgID( LPCOLESTR lpszProgID, //Pointer to the ProgID
LPCLSID pclsid //Pointer to the CLSID
); |
|
|
|
|
|